package com.project.mvprxsample.net;

import android.content.Context;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * retrofit工具类
 */
public class RetrofitUtils {
    private Context mContext;
    private OkHttp3Utils mOkHttp3Utils;
    //服务器路径
    public final String API_SERVER = "https://hms.cignacmb.com/cigna_community/0/0";
    private Retrofit mRetrofit;
    private OkHttpClient mOkHttpClient;

    public RetrofitUtils(Context context) {
        this.mContext = context;
        mOkHttp3Utils = new OkHttp3Utils(context);
    }



    /**
     * 获取Retrofit对象
     *
     * @return
     */
    public Retrofit getRetrofit() {

        if (null == mRetrofit) {
            if (null == mOkHttpClient) {
                mOkHttpClient = mOkHttp3Utils.getOkHttpClient();
//                mOkHttpClient = new OkHttpClient();
            }

            //Retrofit2后使用build设计模式
            mRetrofit = new Retrofit.Builder()
                    //设置服务器路径
                    .baseUrl(API_SERVER)
                    //添加转化库，默认是Gson
                    .addConverterFactory(GsonConverterFactory.create())
//                    //添加回调库，采用RxJava
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    //设置使用okhttp网络请求
                    .client(mOkHttpClient)
                    .build();
        }

        return mRetrofit;
    }

}
